package com.piece.core.framework.util.collection;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class MapBuilder<K, V> {

    private final Map<K, V> map;

    public static <K, V> MapBuilder<K, V> create() {
        return create(false);
    }

    public static <K, V> MapBuilder<K, V> create(boolean isLinked) {
        int initialCapacity = (int)(16 / 0.75F) + 1;
        return create(isLinked ? new LinkedHashMap(initialCapacity) : new HashMap(initialCapacity));
    }

    public static <K, V> MapBuilder<K, V> create(Map<K, V> map) {
        return new MapBuilder(map);
    }

    public MapBuilder(Map<K, V> map) {
        this.map = map;
    }

    public MapBuilder<K, V> put(K k, V v) {
        this.map.put(k, v);
        return this;
    }

    public MapBuilder<K, V> putAll(Map<K, V> map) {
        this.map.putAll(map);
        return this;
    }

    public Map<K, V> map() {
        return this.map;
    }

    public Map<K, V> build() {
        return this.map();
    }

    public String join(String separator, String keyValueSeparator) {
        return MapUtil.join(this.map, separator, keyValueSeparator, new String[0]);
    }

    public String joinIgnoreNull(String separator, String keyValueSeparator) {
        return MapUtil.joinIgnoreNull(this.map, separator, keyValueSeparator, new String[0]);
    }

    public String join(String separator, String keyValueSeparator, boolean isIgnoreNull) {
        return MapUtil.join(this.map, separator, keyValueSeparator, isIgnoreNull, new String[0]);
    }
}